<div id="goods_list" <{if empty($conf.objs)}> style="display:none" <{/if}>>
  <input type="hidden" name="objtype[]" value="goods"/>
  <h5 style="text-align:center;">商品</h5>
  <table class="gridlist" width="100%" cellspacing="0" cellpadding="0" border="0" >
    <thead>
      <tr>
        <th>货号</th>
        <th>商品名称</th>
        <th>规格</th>
        <th>购买数量</th>
        <th>商品价格</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <{foreach from=$conf.objs item=obj}>
      <{if $obj.order_items|@count > 1}>
      <tr class="ColColorGray" id="tr_<{$obj.obj_id}>" oid="<{$obj.obj_id}>" <{if $item.delete == 'true'}>style="background-color: #DBDBDB;" g-del="b"<{else}>g-del="a"<{/if}>>
        <td class="goods_bn"><{$obj.bn}></td>
        <td class="goods_name"><{$obj.name}></td>
        <td class="goods_addon">商品</td>
        <td class="goods_num">-</td>
        <td class="goods_price">-</td>
        <td class="goods_op"></td>
      </tr>
      <{/if}>
      <{foreach from=$obj.order_items item=item}>
      <tr id="tr_<{$item.item_id}>" oid="<{$obj.obj_id}>" <{if $item.delete == 'true'}>style="background-color: #DBDBDB;" g-del="b"<{else}>g-del="a"<{/if}> g-type="<{$item.item_type}>" g-pid="<{$item.product_id}>" g-shop-pid="<{$item.shop_product_id}>" aid="<{$item.item_id}>" g-bn="<{$item.bn}>" g-pname="<{$item.name}>">
        <td class="item_bn <{$item.item_type}>class">
          <span class="number"><{$item.bn}></span>
          <input type="hidden" value="<{$item.bn}>" name="bn_list[]" /></td>
        <td class="item_name"><{$item.name}></td>
        <td class="item_addon"><{$item.addon}></td>
        <td class="item_num"><input type="hidden" value="<{$item.item_id}>" name="goods[obj][<{$obj.obj_id}>][]" />
          <input atype="num" aid="<{$item.item_id}>" name="goods[num][<{$item.item_id}>]" type="text" size="5" value="<{$item.quantity}>" vtype="required&&unsignedint" id="num_<{$item.item_id}>" <{if $item.delete == 'true'}>disabled="true"<{/if}> onchange="total();" /></td>
        <td class="item_price"><input name="goods[price][<{$item.item_id}>]" type="text" size="8" value="<{$item.price}>" vtype="required&&unsigned" id="pr_<{$item.item_id}>" <{if $item.delete == 'true'}>disabled="true"<{/if}> onchange="total();" /></td>
        <td class="item_op" objid="<{$obj.obj_id}>">
          <{if $item.delete == 'false'}>
          <{button type="button" label="删除" id="d_{$item.item_id}" onclick="del_goods('{$item.item_id}')" }></td>
        <{else}>
        <{button type="button" label="恢复" id="d_{$item.item_id}" onclick="del_goods('{$item.item_id}')" }></td>
      <{/if}>
      </tr>
      <{/foreach}>
    <{/foreach}>
    <tr><td colspan="6" style="background:#f0f0f0;height:10px;"></td></tr>
  </tbody>
</table>
</div>
<script>
if($$('.adjunctclass,.giftclass').length){
    $$('.adjunctclass,.giftclass').each(function(e){
      var div = new Element('div').inject(e,'top');
      new Element('div').inject(div);
    });
}
$('add_product').addEvent('click',function(e){
    var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent('index.php?app=ome&ctl=admin_order&act=findProduct');
    Ex_Loader('modedialog',function() {
      new finderDialog(url,{params:{url:'index.php?app=ome&ctl=admin_order&act=getProducts',name:'product_id[]'},handle:this,width:1000,height:660,
          onCallback:function(rs){
              if(!rs)return;
              rs=JSON.decode(rs);
              init(rs);
          }
      });
    }.bind(this));
});
var callurl='index.php?app=ome&ctl=admin_order&act=getProducts',store=[];
var options={
       'getVar':'bn',
       'fxOptions':false,
       callJSON:function(){return window.autocompleter_json;},
       injectChoice:function(json){
           var choice = new Element('li', {'html': this.markQueryValue(json[this.options.getVar])});
           choice.store('_data',json);
           choice.inputValue = json[this.options.getVar];
           this.addChoiceEvents(choice).inject(this.choices);
       },
       onHide:function(){
               if(!this.selected)return ;
              var _json=this.selected.retrieve('_data');
               _json=$splat(_json);
               init(_json);
               MessageBox.success('加载商品成功!');
      },onFocus:function(ipt){
               ipt.value='';
      }
}
Ex_Loader('autocompleter',function() {
  new Autocompleter.script($E('#pfba input'),callurl, options);
  new Autocompleter.script($E('#pfba2 input'),callurl,$merge(options,{'getVar':'name'}));
});

function init(rs){
    rs.each(function (i){
        var pid = i.product_id,ishave='false',aid='';
        $ES('#goods_list tbody tr').each(function (e){
            if (e.get('g-pid') == pid && e.get('g-type') == 'product'){
                ishave='true';
                aid=e.get('aid');
            }
        });
        if (ishave=='true'){
            if ($("newnum_"+pid)){
                if ($("newnum_"+pid).disabled){
                    alert("请恢复货品："+i.name);
                }else {
                    alert("有此货品："+i.name);
                    $("newnum_"+pid).focus();
                }
                return ;
            }else {
                if ($("num_"+aid).disabled){
                    alert("请恢复货品："+i.name);
                }else {
                    alert("有此货品："+i.name);
                    $("num_"+aid).focus();
                }
                return ;
            }
        }else {
            create(pid,i);
        }
    });
    if ($('goods_list').style.display=='none'){
        $('goods_list').style.display = "";
    }
};

function create(pid,rs){
    var trr = new Element('tr', {id:'trp_'+pid}).inject($E('#goods_list tbody'));
    trr.setProperty('g-pid',pid);
    trr.setProperty('g-bn',rs.bn);
    trr.setProperty('g-pname',rs.name);
    trr.setProperty('g-type','product');
    new Element('td', {html:rs.bn}).inject(trr);
    new Element('td', {html:rs.name}).inject(trr);
    new Element('td', {html:rs.spec_info}).inject(trr);
    var td1 = new Element('td').inject(trr);
    var td2 = new Element('td').inject(trr);
    var td3 = new Element('td').inject(trr);
    var tmp_bn = new Element('input[type=hidden]', {name:'bn_list[]', value:rs.bn}).inject(td1);
    var tmp = new Element('input[type=text]', {id:'newnum_'+pid, vtype:'number&&unsignedint', name:'goods[newnum]['+pid+']', value:"1", size:'5',events:{'change':function(){total();}}}).inject(td1);
    tmp.setProperty('atype','newnum');
    tmp.setProperty('aid',pid);
    new Element('input[type=text]', {id:'newpr_'+pid, vtype:'number&&unsigned', name:'goods[newprice]['+pid+']', value:"0", size:'8',events:{'change':function(){total();}}}).inject(td2);
    new Element('button.btn', {id:'btn_'+pid, type:'button', html:'<span><span>删除</span></span>',events:{'click':function(){del_goods_new(pid);}}}).inject(td3);
}

function total_goods(){
    var total = 0;
    $('goods_list').getElements("input[type=text]").each(function (e){
        if (!e.disabled){
            if (/^\d+(\.\d+)?$/.test(e.value)){
                if (e.get('atype') == 'num'){
                    if (parseInt(e.value) <= 0){
                        var _ca = e.getNext();
                        if (!_ca) new Element('span',{'class':"error caution notice-inline",html:"请录入不小于0的数"}).injectAfter(e);
                    }else {
                        var _ca = e.getNext();
                        if (e.value > 0){
                            if (_ca) _ca.remove();
                        }
                    }
                    id = e.get('aid');
                    total += parseFloat(e.value).toFixed(3)*parseFloat($('pr_'+id).value).toFixed(3);
                }else if (e.get('atype') == 'newnum'){
                    if (parseInt(e.value) <= 0){
                        var _ca = e.getNext();
                        if (!_ca) new Element('span',{'class':"error caution notice-inline",html:"请录入不小于0的数"}).injectAfter(e);
                    }else {
                        var _ca = e.getNext();
                        if (e.value > 0){
                            if (_ca) _ca.remove();
                        }
                    }
                    id = e.get('aid');
                    total += parseFloat(e.value).toFixed(3)*parseFloat($('newpr_'+id).value).toFixed(3);
                }else if (e.get('atype') == 'num_obj'){
                    if (parseInt(e.value) == 0){
                        var _ca = e.getNext();
                        if (!_ca) new Element('span',{'class':'error caution notice-inline',html:'请录入不小于0的数'}).injectAfter(e);
                    }else {
                        var _ca = e.getNext();
                        if (e.value > 0){
                            if (_ca) _ca.remove();
                        }
                    }
                    id = e.get('aid');
                    total += parseFloat(e.value).toFixed(3)*parseFloat($('pkgpr_'+id).value).toFixed(3);
                }
            }
            else{
              var _ca = e.getNext();
              if (!_ca) new Element('span',{'class':"error caution notice-inline",html:"请录入不小于0的数"}).injectAfter(e);
            }
        }
      });
      return total;
  }
  function del_goods(id){
    var oid = $("tr_"+id).get('oid');

    if($('tr_'+id).get('g-del') == 'a'){
      if($("tr_"+id).get('g-type') == 'product'){
        $$('#goods_list tbody tr[oid='+oid+']').each(function(e){
          e.setStyle('background-color','#D8D8D8').set('g-del','b');
          e.getElements('input').length && e.getElements('input[type=text]').set('disabled',true);
          e.get('g-type') == 'adjunct' || e.get('g-type') == 'gift' ? e.getElement('button').hide() : e.getElement('button') && e.getElement('button').getElement('span').getElement('span').set('text','恢复');
        });
      }
      else {
        $("num_"+id).disabled=true;
        $("pr_"+id).disabled=true;
        $("tr_"+id).setStyle('background-color','#D8D8D8').set('g-del','b');
        $("d_"+id).getElement('span').getElement('span').set('text','恢复');
      }
    }
    else if($('tr_'+id).get('g-del') == 'b'){
      if($("tr_"+id).get('g-type') == 'product'){
        $$('#goods_list tbody tr[oid='+oid+']').each(function(e){
          e.setStyle('background-color','').set('g-del','a');
          e.getElements('input').length && e.getElements('input[type=text]').set('disabled',false);
          e.getElement('button') && e.getElement('button').show().getElement('span').getElement('span').set('text','删除');
        });
      }
      else {
        $("num_"+id).disabled=false;
        $("pr_"+id).disabled=false;
        $("tr_"+id).setStyle('background-color','').set('g-del','a');
        $("d_"+id).getElement('span').getElement('span').set('text','删除');
      }
    }
    total();
  }

//删除新增商品
  function del_goods_new(id){
      $("trp_"+id).remove();
      total();
      if (!$E('#pkg_list tbody').getElement('td')){
          $('pkg_list').style.display = "none";
      }
  }

</script>
